<template>
  <div class="app-container">
    <!--工具栏-->
    <div>
      <el-form ref="queryForm" :inline="true" label-width="75px">
        <el-row>
          <el-form-item label="库名">
            <el-input v-model="query.schemaName" placeholder="请输入库名" @keyup.enter.native="crud.toQuery" />
          </el-form-item>
          <el-form-item label="表名">
            <el-input v-model="query.scriptName" placeholder="请输入表名" @keyup.enter.native="crud.toQuery" />
          </el-form-item>
          <rrOperation :crud="crud" />
        </el-row>
      </el-form>
    </div>
    <crudOperation :permission="permission" />
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      style="width: 100%;"
      :data="crud.data"
      @filter-change="crud.handleFilterChange"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column v-if="columns.visible('systemName')" label="系统名" prop="systemName" />
      <el-table-column v-if="columns.visible('schemaName')" label="库名" prop="schemaName" />
      <el-table-column v-if="columns.visible('jobName')" label="作业名" prop="jobName" min-width="150" />
      <el-table-column v-if="columns.visible('tableName')" label="表名" prop="tableName" min-width="150" />
      <el-table-column v-if="columns.visible('isRule')" prop="isRule" label="是否有规则">
        <template slot-scope="scope">
          {{ formatterIsRule(scope.row.isRule) }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('crtTs')" label="创建时间" prop="crtTs" min-width="150" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="rulesMaintain(scope.row)">检查规则维护</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch></el-switch>
        </template>
      </el-table-column> -->
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--新增/编辑弹窗-->
    <el-dialog
      v-if="crud.status.cu > 0 || detailsFlag === true"
      append-to-body
      :close-on-click-modal="false"
      :modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0 || detailsFlag === true"
      :title="crud.status.title"
      width="900px"
      top="15vh"
    >
      <el-form ref="taskForm" :inline="true" :rules="rules" :model="form" label-width="150px">
        <dqc-work-manage-list :form="form" />
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="crud.cancelCU()">关闭</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </div>
    </el-dialog>
    <!--  检查规则维护弹窗  -->
    <el-dialog
      v-if="fieldFlag"
      append-to-body
      :visible.sync="fieldFlag"
      :close-on-click-modal="false"
      title="编辑检查任务信息"
      width="1400px"
      top="5vh"
    >
      <el-form ref="fieldMaps" :model="form" :inline="true" label-width="150px">
        <dqc-task-list :form="form" />
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="fieldClose()">取消</el-button>
        <el-button type="primary" @click="fieldSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/expand/dqc/CRUD.operation.vue'
import pagination from '@crud/Pagination.vue'
import dqcConfigApi from '@/api/dqc/dqcConfig'
import dqcWorkManageList from './dqc-work-manage-list.vue'
import dqcTaskList from './dqc-task-list.vue'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '检查作业管理信息', url: '/api/dqc/task', crudMethod: { ...dqcConfigApi }})
const defaultForm = {
  pkId: null,
  systemName: 'GAUSS',
  dataLink: '',
  sourceConnPkId: '',
  targetConnPkId: '',
  sourceDbName: '',
  sourceTableName: '',
  schemaName: '',
  tableName: '',
  taskName: null,
  sourceConnCode: null,
  targetConnCode: null,
  dbName: null,
  ruleType: '',
  expectedTime: '',
  inspectCondition: '',
  inspectFrequency: 'day',
  sendRank: '',
  runMode: '',
  alarmStartDate: '',
  isRule: false
}
export default {
  name: 'ScriptConvertTask',
  components: {
    dqcTaskList,
    dqcWorkManageList,
    crudOperation,
    rrOperation,
    pagination
  },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      fieldFlag: false,
      detailsFlag: false,
      permission: {
        add: ['admin', 'dqc:add'],
        edit: ['admin', 'dqc:edit'],
        del: ['admin', 'dqc:del']
      },
      rules: {
        systemName: [
          { required: true, message: '请选择系统名', trigger: 'change' }
        ],
        sourceConnPkId: [
          { required: true, message: '请选择数据连接', trigger: 'change' }
        ],
        schemaName: [
          { required: true, message: '请选择数据库', trigger: 'change' }
        ],
        tableName: [
          { required: true, message: '请选择表', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.crud.resetQuery(false)
  },
  methods: {
    rulesMaintain(row) {
      console.log('行数据', row)
      this.form.isRule = row.isRule
      this.form.pkId = row.pkId
      this.form.schemaName = row.schemaName
      this.form.sourceConnPkId = row.sourceConnPkId
      this.form.tableName = row.tableName
      this.form.ruleType = row.ruleType
      this.form.expectedTime = row.expectedTime
      this.form.inspectCondition = row.inspectCondition
      this.form.inspectFrequency = row.inspectFrequency
      this.form.sendRank = row.sendRank
      this.form.runMode = row.runMode
      this.form.alarmStartDate = row.alarmStartDate
      this.form.isRule = true
      // this.form = row
      this.fieldFlag = true
    },
    save() {
      this.$refs['taskForm'].validate((valid) => {
        if (valid) {
          if (this.form.pkId != null) {
            dqcConfigApi.edit(this.form).then(rsp => {
              this.crud.cancelCU()
              this.crud.refresh()
            })
          } else {
            this.crud.doAdd()
          }
        } else {
          return false
        }
      })
    },
    fieldClose() {
      this.form.isRule = ''
      this.form.pkId = ''
      this.form.schemaName = ''
      this.form.sourceConnPkId = ''
      this.form.tableName = ''
      this.form.ruleType = ''
      this.form.expectedTime = ''
      this.form.inspectCondition = ''
      this.form.inspectFrequency = ''
      this.form.sendRank = ''
      this.form.runMode = ''
      this.form.alarmStartDate = ''
      this.fieldFlag = false
    },
    fieldSave() {
      this.$refs['fieldMaps'].validate((valid) => {
        if (valid) {
          dqcConfigApi.updateRules(this.form).then(rsp => {
            this.crud.cancelCU()
            this.crud.refresh()
            this.$nextTick(() => {
              this.fieldFlag = false
            })
          })
        } else {
          return false
        }
      })
    },
    formatterIsRule(val) {
      if (val === false) {
        return '无'
      } else if (val === true) {
        return '有'
      } else {
        return '无'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

